import base64
from urllib.parse  import unquote,quote

def base_decode(filename):    
    with open(filename,'r')  as f:
        data = f.read()
   
    for i in range(30):  
        print(i) 
        data = unquote(data)
        try:
            data = base64.b16decode(data)
            print('base16 decode correct')
        except Exception as e:
            print('base16 decode error...',end='')
            print(e)
            try:
                data = base64.b32decode(data)
                print('base32 decode correct')
            except Exception as e:
                print('base32 decode error...',end='')
                print(e)
                try:
                    data = base64.b64decode(data)
                    print('base64 decode correct')
                except Exception as e:
                    print('base64decode error...',end='')
                    print(e)
                    exit()
        data =str(data,'utf8')
        print('........    %s'%data[-100:])
        print('===================================')

def  base_code(filename):
    data = '.- -.. . .- ..-. . ..-. -...'
    for i in range(23):
        print(i) 
        data = data.encode('utf-8')
        if i ==  10  or  i == 15:
            data=base64.b16encode(data)
        elif i == 5  or  i  == 12 or  i == 18:
            data=base64.b32encode(data)
        else:
            data=base64.b64encode(data)
        data = str(quote(data))
        print('........    %s'%data[-100:])
    with open(filename,'w')  as f:
        f.write(data)

if __name__  == '__main__':
    filename=r'f:\key1.txt'
    #base_code(filename)
    base_decode(filename)